using System;
using System.Collections.Generic;
using Function.SerializedPropertyAttribute;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.WindowDraw
{
    public abstract class EPageView<T> : ECommonView where T : NCommonView
    {
        [Head("一页显示几个")] public int _pageCount;
        [Head("文字色")] public Color _textColor;
        [Head("文字选中色")] public Color _selectTextColor;
        [Head("选中背景色")] public Color _selectBgColor;
        [Head("背景色")] public Color _bgColor;
        [HideInInspector] [Tooltip("搜索")] public string _searchText = string.Empty;
        [HideInInspector] [Tooltip("当前页签")] public int _curPage;
        [HideInInspector] [Tooltip("当前选择的")] public int _curSelect;
        [HideInInspector] [Tooltip("当前展示的")] public List<T> _showMap;
        [HideInInspector] [Tooltip("所有的")] public List<T> _pageMap;
        [HideInInspector] private int _id;
        private string _errorText;

        public override void OnEnable()
        {
            _curSelect = -1;
            _curPage = 0;
        }

        public override void OnGUI()
        {
            GUILayout.BeginHorizontal();
            // 放大镜图标
            Texture2D magnifierIcon = EditorGUIUtility.IconContent("Search Icon").image as Texture2D;
            GUILayout.Label(magnifierIcon, GUILayout.Width(16), GUILayout.Height(16));

            // 输入框
            var searchText = GUILayout.TextField(_searchText, GUILayout.MinWidth(150));
            _searchText = searchText ?? string.Empty;
            GUILayout.Space(10f);
            EditorGUILayout.EndHorizontal();
            _pageMap = GetPageMap();
            for (int index = _pageMap.Count - 1; index >= 0; index--)
            {
                _pageMap[index].UpdateArea(Rect.zero);
            }

            _showMap = GetShowTime(_pageMap, _searchText);
            for (int index = _curPage * _pageCount; index <= (_curPage + 1) * _pageCount - 1; index++)
            {
                if (index <= _showMap.Count - 1)
                {
                    GUIStyle boxStyle = EditorDrawHelp.GetBoxStyle(_bgColor);
                    if (_curSelect != -1)
                    {
                        boxStyle = _showMap[index] == _pageMap[_curSelect]
                            ? EditorDrawHelp.GetBoxStyle(_selectBgColor)
                            : boxStyle;
                    }

                    GUILayout.BeginVertical(boxStyle);
                    _showMap[index].OnGUI();
                    GUILayout.EndVertical();
                    var rect = GUILayoutUtility.GetLastRect();
                    if (rect != Rect.zero && rect != new Rect(0, 0, 1, 1))
                    {
                        _showMap[index].UpdateArea(rect);
                    }
                }
                else
                {
                    break;
                }
            }

            GUILayout.FlexibleSpace();


            //add
            if (!_errorText.IsNullOrEmpty())
            {
                GUILayout.Label(_errorText,
                    EditorDrawHelp.GetTextGUIStyle(15, TextAnchor.MiddleCenter, EditorDrawHelp._firebrick1));
            }

            GUILayout.BeginHorizontal();
            _id = EditorDrawHelp.DrawIntField("ID", _id, 150);
            if (GUILayout.Button("+"))
            {
                GUI.FocusControl(null);
                if (AddItem(_id))
                {
                    _id = 0;
                    _errorText = "";
                }
                else
                {
                    _errorText = $"{_id} is exist , please change id";
                }
            }

            GUILayout.EndHorizontal();


            GUILayout.Label("-------------------------------");
            //page
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(EditorContentHelp.PreviousFrameContent))
            {
                ReducePage();
            }

            GUILayout.Label($"{_curPage}/{GetMaxPage()}",
                EditorDrawHelp.GetTextGUIStyle(15, TextAnchor.LowerCenter, Color.black));
            if (GUILayout.Button(EditorContentHelp.NextFrameContent))
            {
                AddPage();
            }

            GUILayout.EndHorizontal();
            GUILayout.Label("-------------------------------");
        }

        public override void UpdateEvent(Event e)
        {
            for (int offest = _showMap.Count - 1; offest >= 0; offest--)
            {
                _showMap[offest].UpdateEvent(e);
            }


            base.UpdateEvent(e);
        }

        protected abstract List<T> GetPageMap();

        protected abstract List<T> GetShowTime(List<T> pageMap, string search);

        protected abstract bool AddItem(int id);

        public int GetMaxPage()
        {
            var pageMap = GetPageMap();
            return (int)MathF.Ceiling(pageMap.Count / _pageCount);
        }

        public T GetCurSelect()
        {
            if (_curSelect >= 0 && _curSelect <= _pageMap.Count - 1)
                return _pageMap[_curSelect];
            return null;
        }

        public void ReducePage()
        {
            if (_curPage >= 1)
            {
                _curPage -= 1;
            }
        }

        public void AddPage()
        {
            if (_curPage < GetMaxPage())
            {
                _curPage += 1;
            }
        }

        public void DelItem(T item)
        {
            _pageMap.Remove(item);
            _showMap.Remove(item);
            _curSelect = -1;
        }

        public void SelectItem(T item)
        {
            for (int index = 0; index < _pageMap.Count; index++)
            {
                if (item == _pageMap[index])
                {
                    _curSelect = index;
                    return;
                }
            }
        }
    }
}